package v.book.read.tip.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Foo {

    void f(String s);

    void g(int i);

    String h(int i, String s);

}

/**
 * 按照这种说话，那我们经常用的 intserface -> impl 模式，不都是代理模式？
 * @author V
 *
 */
public class DynamicProxyDemo {

    public static void main(String[] args) {
        Foo prox =
                (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(), new Class[] {Foo.class},
                        new InvocationHandler() {

                            public Object invoke(Object proxy, Method method, Object[] args)
                                    throws Throwable {
                                System.out.println("InvocationHandler called:" + "\n\tMethod = "
                                        + method);
                                if (args != null) {
                                    System.out.println("\targs = ");
                                    for (int i = 0; i < args.length; i++)
                                        System.out.println("\t\t" + args[i]);
                                }
                                return null;
                            }

                        });
        // System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true);
        prox.f("hello");
        prox.g(47);
        prox.h(47, "hello");
        prox.h(47, "hello");
    }

}
